package com.qf.service;

import com.netflix.ribbon.proxy.annotation.Http;
import com.qf.config.IdWorker;
import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.pojo.SmsInterfaceForm;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lixu
 */
@Service
public class SmsInterfaceServiceImpl  implements SmsInterfaceService{

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;

    public static final Integer HTTPREQUEST = 1;
    public static final Integer WEBREQUEST = 2;

    /**
     * 		把多个手机号的发送信息，拆为按手机号每条发送到队列。
     * @param form
     */
    @Override
    public void sendMQ(SmsInterfaceForm form) {

        //手机号集合
        String mobile = form.getMobile();
        String[] ms = mobile.split(",");
        for (String m : ms) {
            StandardSubmit standardSubmit = new StandardSubmit();//唯一ID 服务端
            //1:分布式ID生成器  雪花算法
            long id = idWorker.nextId();
            standardSubmit.setSequenceId(String.valueOf(id));
            //2:
            standardSubmit.setClientID(Integer.parseInt(form.getClientID()));
            //3:
            standardSubmit.setSrcSequenceId(Long.parseLong(form.getSrcID()));
            //4:
            standardSubmit.setDestMobile(m);
            //5:
            standardSubmit.setMessageContent(form.getContent());
            //6:
            standardSubmit.setSource(HTTPREQUEST);
            //7:发消息
            rabbitTemplate.convertAndSend(RabbitMQConstants.TOPIC_PRE_SEND_EXCHANGE,"sms",
                    standardSubmit
                    );
        }
    }
}
